package com.edu.neusoft.elec_shop.controller;

import com.edu.neusoft.elec_shop.domain.Orders;
import com.edu.neusoft.elec_shop.service.OrdersService;
import com.edu.neusoft.elec_shop.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/orders")
@Api(tags = "订单管理接口")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    // 添加订单信息
    @PostMapping
    @ApiOperation(value = "添加订单信息", notes = "接收订单对象作为请求体，将订单信息添加到数据库中")
    public AjaxResult insert(@RequestBody @Valid Orders record) {
        try {
            int result = ordersService.insert(record);
            if (result > 0) {
                return AjaxResult.success("订单添加成功");
            } else {
                return AjaxResult.error("订单添加失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("订单添加失败：" + e.getMessage());
        }
    }

    // 根据订单号删除订单信息
    @DeleteMapping("/{orderId}")
    @ApiOperation(value = "根据订单号删除订单信息", notes = "根据传入的订单号，删除对应的订单信息")
    public AjaxResult deleteOrderById(@PathVariable("orderId") @Valid Long orderId) {
        try {
            int result = ordersService.deleteOrderById(orderId);
            if (result > 0) {
                return AjaxResult.success("订单删除成功");
            } else {
                return AjaxResult.error("订单删除失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("订单删除失败：" + e.getMessage());
        }
    }
}